<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Ionize, creative CMS
 *
 * @package		Ionize
 * @author		Ionize Dev Team
 * @license		http://ionizecms.com/doc-license
 * @link		http://ionizecms.com
 * @since		Version 0.9.0
 */

// ------------------------------------------------------------------------

/**
 * Ionize, creative CMS Article Controller
 *
 * @package		Ionize
 * @subpackage	Controllers
 * @category	Article
 * @author		Ionize Dev Team
 *
 */
 
class pr_category_cl extends MY_admin 
{

	/**
	 * Fields on wich the htmlspecialchars function will not be used before saving
	 * 
	 * @var array
	 */
	/**
	 * Fields on wich no XSS filtering is done
	 * 
	 * @var array
	 */
	/**
	 * Constructor
	 *
	 */
	public function __construct()
	{
		parent::__construct();
		// product model
		$this->load->model('pr_fnc_model', '', true);
		//$this->load->model('sibilling_model', '', true);
	}

	// ------------------------------------------------------------------------
	/**
	 * Default : Do nothing
	 *
	 */
	function index()
	{
		//point to pr_tab_categroy view, then pr_tab_categroy point to category_info panel
		$data=$this->pr_fnc_model->get_all('tbl_category');
		$this->template['category_data']=$data;
		$this->output('pr_tab_category');
	}
	
	// get List product
	function get_list_pagination($page=1)
	{
		//$branchs	= $this->sibilling_model->get_by_field('branchs', 'id', $this->session->userdata('branchs_id'));
		if($this->input->post('num') != ''){
			$num = $this->input->post('num');
			$nb = ($this->input->post('nb')) ? $this->input->post('nb') : $num;
		}else{
			$num = 25;
			$nb = ($this->input->post('nb')) ? $this->input->post('nb') : $num;
		}
		
		$txt_filter = $this->input->post('keyword');
		
		if($txt_filter == ''){
			$keyword = '';
			$this->template['keyword'] = '';
		}else{
			$num = 25;
			$this->template['keyword'] = $txt_filter;
			$keyword = "WHERE lower(cat_name) like '%".strtolower($txt_filter)."%'";
		}
		//trace($txt_filter);
		// Minimum
		if ($nb < $num)	$nb = $num;
		
		$page 	= $page - 1;
		$offset = $page * $nb;
		
		$sql	= 	"SELECT * FROM tbl_category ".$keyword."
					ORDER BY cat_id DESC
					LIMIT ".$nb. " OFFSET ".$offset ;
			
		//trace($sql);				
		
		$category = $this->pr_fnc_model->get_by_sql($sql);
		$this->template['category'] = $category;
		//trace($odf_list);
		
		// Pagination
		$this->template['current_page'] = $page + 1;
		$this->template['nb'] = $nb;
		$this->template['option'] = $num;
		$this->template['category_count'] = $this->pr_fnc_model->count_by_sql("SELECT * FROM tbl_category ORDER BY cat_id DESC");
		
		$this->template['category_list_pages'] = ceil($this->template['category_count'] / $nb);
		$this->template['paginations']=$this->connect->paginations($this->template['category_list_pages'],$this->template['current_page']);
		
		$this->output('pr_tab_category');
	}
	
	function page_category_new_submit(){	
		$this->template['categorys']	= $this->pr_fnc_model->get_by_sql('SELECT * FROM tbl_category ORDER BY cat_id DESC');	
		$this->output('pr_tab_category_new_submit');
	}
	function page_insert_category_submit()
	{
		if ($this->input->post('pd_name') != '' )
		{
				$data = array(		
							  'cat_name'		=> $this->input->post('pd_name'),	
							  'cat_des'	=> $this->input->post('pd_des'),						  	
							  'cat_image'	=> $this->input->post('pd_image'),
							  );
				// Insert
				$affected_rows = $this->pr_fnc_model->insert('tbl_category',$data);

				if ($affected_rows > 0)
				{
					// UI update panels
					$this->update[] = array(
						'element' => 'w1_contentWrapper',
						'url' => admin_url() . 'pr_category_cl/page_category_new_submit'
					 );
					 // Success message
					$this->success(lang('pr_message_new'));
				} 
		}//end if check textbox can't null
		else
		{
			$this->error(lang('pr_error_submit_form'));
		}
	}//end function
	function page_category_new(){		
		$this->output('pr_tab_category_new');
	}
	function page_insert_category()
	{
		if ($this->input->post('pd_name') != '' )
		{
				$data = array(		
							  'cat_name'		=> $this->input->post('pd_name'),	
							  'cat_des'	=> $this->input->post('pd_des'),						  	
							  'cat_image'	=> $this->input->post('pd_image'),
							  );
				// Insert
				$affected_rows = $this->pr_fnc_model->insert('tbl_category',$data);

				if ($affected_rows > 0)
				{
					// UI update panels
					$this->update[] = array(
						'element' => 'category_info',
						'url' => admin_url() . 'pr_category_cl/get_list_pagination'
					 );
					 // Success message
					$this->success(lang('pr_message_new'));
				} 
		}//end if check textbox can't null
		else
		{
			$this->error(lang('pr_error_submit_form'));
		}
	}//end function
	
	//show data to edit
	function page_edit_category($id){
		$category_data	= $this->pr_fnc_model->get_by_sql("SELECT * FROM tbl_category WHERE cat_id=".$id);			
		$this->template['category_data']	= $category_data;		
		$this->output('pr_tab_category_edit');
	}
	function page_update_category($cat_id)
	{					
		$data = array(
				'cat_name'=> $this->input->post('cat_name'),
				'cat_des'=> $this->input->post('cat_des'),
				'cat_image'=> $this->input->post('cat_image')			
				 );

				$affected_rows = $this->pr_fnc_model->save_edit('tbl_category',$data,'cat_id',$cat_id);
				///trace($affected_rows);
				if ($affected_rows > 0)
				{
					// UI update panels
					$this->update[] = array(
						'element' => 'category_info',
						'url' => admin_url() . 'pr_category_cl/get_list_pagination'
					 );
					 
					// Success message
					$this->success(lang('pr_message_new'));
				}
				else
				{
					$this->error('Fail!..');
				}
	}//end function
	
	/// function remove POP / POLL location
	function delete_categroy($id){
		$data = array( 'cat_id' => $id);
		$affected_rows = $this->pr_fnc_model->delete('tbl_category',$data);

		if ($affected_rows > 0)
		{
			// UI update panels
			$this->update[] = array(
				'element' => 'category_info',
				'url' => admin_url() . 'pr_category_cl/get_list_pagination'
			);
			$this->success('Successful!..');
		}
		else
		{
			$this->error('Fail!..');
		}	
	}
	
	/**
	 * view_picture
	 *
	 */
	function view_picture($picture=NULL, $zoom=NULL)
	{

		if($zoom == NULL)
			echo '<img width="115px" height="115px" src="'.base_url().'files/pictures/'.$picture.'" /><input type="hidden" id="picture_name" value="'.$picture.'" />';
		else
			echo '<img width="100%" height="100%" src="'.base_url().'files/pictures/'.$picture.'" />';
	}
	
	/**
	 * upload_file
	 *
	 */
	function upload_file()
	{		
		//////////////////////File//////////////////////////////////
		$file_element_name = 'userfile';
		
		$this->gallery_path = str_replace('\\','/',realpath(APPPATH. '../files/pictures'));
		$config = array(
										//'file_name'    	=> time().'_'.'New Name',//pic name
										'allowed_types' 	=> 'jpeg|gif|jpg|png|bmp',
										'upload_path' 		=> $this->gallery_path,
										'max_size' 			=> 5120
									);
		
		$this->load->library('upload', $config);
	
		if(!$this->upload->do_upload($file_element_name)){
			echo 'Upload fail';
		}
		else
		{
			$image_data = $this->upload->data();
			echo $image_data['file_name'];
		}
	}
	
	/**
	 * Delete file
	 *
	 */
	function delete_file()
	{
		if ($this->input->post('picture')	!= '')
		{
			$this->gallery_path = str_replace('\\','/',realpath(APPPATH. '../files/pictures'));
			unlink($this->gallery_path.'/'.$this->input->post('picture'));
		}
	}
	
}


/* End of file article.php */
/* Location: ./application/controllers/admin/article.php */